package com.ysd.lis.service.bac.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.bac.BacStainingConclusion;
import com.ysd.lis.entity.bac.BacStainingMethod;
import com.ysd.lis.entity.bac.BacStainingOptions;
import com.ysd.lis.entity.sta.StaStainingConclusion;
import com.ysd.lis.entity.sta.StaStainingMethod;
import com.ysd.lis.entity.sta.StaStainingOptions;
import com.ysd.lis.mapper.bac.BacStainingMethodMapper;
import com.ysd.lis.mapper.sta.StaStainingMethodMapper;
import com.ysd.lis.request.Params;
import com.ysd.lis.service.bac.BacStainingConclusionService;
import com.ysd.lis.service.bac.BacStainingMethodService;
import com.ysd.lis.service.bac.BacStainingOptionsService;
import com.ysd.lis.service.sta.StaStainingConclusionService;
import com.ysd.lis.service.sta.StaStainingMethodService;
import com.ysd.lis.service.sta.StaStainingOptionsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wanghuaixing
 * @since 2024-04-07
 */
@Service
public class BacStainingMethodServiceImpl extends ServiceImpl<BacStainingMethodMapper, BacStainingMethod> implements BacStainingMethodService {

    @Autowired
    BacStainingMethodMapper bacStainingMethodMapper;

    @Autowired
    StaStainingMethodMapper staStainingMethodMapper;
    @Autowired
    StaStainingMethodService staStainingMethodService;
    @Autowired
    StaStainingConclusionService staStainingConclusionService;
    @Autowired
    StaStainingOptionsService staStainingOptionsService;

    @Autowired
    BacStainingConclusionService bacStainingConclusionService;
    @Autowired
    BacStainingOptionsService bacStainingOptionsService;


    @Override
    public Result findStainingMethodList(Params params) {
        LambdaQueryWrapper<BacStainingMethod> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BacStainingMethod::getDelFlag, 0);
        if (!StringUtils.isEmpty(params.getSearchValue())) {
            queryWrapper.and(wrapper -> wrapper.like(BacStainingMethod::getMethodCode, params.getSearchValue()).or().like(BacStainingMethod::getMethodName, params.getSearchValue()));
        }
        List<BacStainingMethod> bacStainingMethodList = bacStainingMethodMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", bacStainingMethodList);
    }

    @Override
    public Result batchSaveStainingMethod(List<BacStainingMethod> bacStainingMethodList) {
        if (bacStainingMethodList.isEmpty()) {
            return Result.fail("没有要更新的项");
        }
        List<BacStainingMethod> nList = bacStainingMethodList.stream().filter(p -> com.baomidou.mybatisplus.core.toolkit.StringUtils.isBlank(p.getId())).collect(Collectors.toList());
        List<BacStainingMethod> uList = bacStainingMethodList.stream().filter(p -> com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(p.getId())).collect(Collectors.toList());
        if (!nList.isEmpty()) {
            boolean r = this.saveBatch(nList);
        }
        if (!uList.isEmpty()) {
            boolean r1 = this.updateBatchById(uList);
        }
        return Result.succ(1, "操作成功", bacStainingMethodList);
    }

    @Override
    public Result batchDeleteStainingMethod(List<BacStainingMethod> bacStainingMethodList) {

        List<String> ids = bacStainingMethodList.stream().map(BacStainingMethod::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result batchUpdateStainingMethod(List<StaStainingMethod> staStainingMethodList) {
        // 取出id
        List<String> ids = staStainingMethodList.stream().map(StaStainingMethod::getId).collect(Collectors.toList());
        List<StaStainingMethod> stainingMethodList = staStainingMethodMapper.selectBatchIds(ids);

        //收集stainingMethodList中的STA_METHOD_NO
        List<String> methodNoList = stainingMethodList.stream().map(StaStainingMethod::getMethodCode).collect(Collectors.toList());

        //根据methodNoList查询出STA_STAINING_CONCLUSION的数据
        List<StaStainingConclusion> staStainingConclusionList = staStainingConclusionService.list(
                new LambdaQueryWrapper<StaStainingConclusion>()
                        .in(StaStainingConclusion::getStaMethodNo, methodNoList)
                        .eq(StaStainingConclusion::getDelFlag, 0));

        //收集staStainingConclusionList中的STA_CONCLUSION_NO
        List<String> conclusionNoList = staStainingConclusionList.stream().map(StaStainingConclusion::getConclusionCode).collect(Collectors.toList());
        //根据conclusionNoList查询出STA_STAINING_OPTIONS的数据
        List<StaStainingOptions> staStainingOptionsList = staStainingOptionsService.list(
                new LambdaQueryWrapper<StaStainingOptions>()
                        .in(StaStainingOptions::getStaConclusionNo, conclusionNoList)
                        .eq(StaStainingOptions::getDelFlag, 0));

        //把查询出的数据复制到bac表里
        for (StaStainingMethod staStainingMethod : staStainingMethodList){
            BacStainingMethod bacStainingMethod = new BacStainingMethod();
            BeanUtil.copyProperties(staStainingMethod,bacStainingMethod,"id");
            this.saveOrUpdate(bacStainingMethod);
        }
        for (StaStainingConclusion staStainingConclusion : staStainingConclusionList){
            BacStainingConclusion bacStainingConclusion = new BacStainingConclusion();
            BeanUtil.copyProperties(staStainingConclusion,bacStainingConclusion,"id");
            bacStainingConclusionService.saveOrUpdate(bacStainingConclusion);
        }
        for (StaStainingOptions staStainingOptions : staStainingOptionsList){
            BacStainingOptions bacStainingOptions = new BacStainingOptions();
            BeanUtil.copyProperties(staStainingOptions,bacStainingOptions,"id");
            bacStainingOptionsService.saveOrUpdate(bacStainingOptions);
        }

        return Result.succ(1, "操作成功", null);


    }
}
